using System;

namespace newtelligence.DasBlog.Web
{
	/// <summary>
	/// Responsible for creating edit controls
	/// </summary>
	public class EditControlProvider
	{
		public EditControlProvider()
		{
		}

		/// <summary>
		/// Creates and returns instance of a <see cref="Core.EditControlAdapter"/> based on the site configuration.
		/// </summary>
		/// <returns>An instance of <see cref="Core.EditControlAdapter"/></returns>
		public Core.EditControlAdapter CreateEditControl()
		{
			SiteConfig siteConfig = SiteConfig.GetSiteConfig();
			string configuredEditControl = siteConfig.EntryEditControl;

			Core.EditControlAdapter editControl = null;

			if (configuredEditControl != null && configuredEditControl.Length > 0)
			{
				Type editControlType = Type.GetType(configuredEditControl, false, false);
				if (editControlType != null)
				{
					//TODO: it might be better to cache the ConstructorInfo reference in a static field
					editControl = Activator.CreateInstance(editControlType) as Core.EditControlAdapter;
				}
			}
			
			// if the configured control cannot be loaded, default to the freetextbox
			if (editControl == null)
			{
				editControl = new FreeTextBoxAdapter();
			}
			return editControl;
		}
	}
}
